<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\ApiController;
use App\Models\Cart;
use Illuminate\Http\Request;

#购物车控制器
class CartController extends ApiController
{
    /**添加购物车
     * @param Request $request
     * @param Cart $cartModel
     * @return mixed
     */
    public function add_cart(Request $request,Cart $cartModel)
    {
        $validator = \Validator::make($request->all(),[
            'goods_id' => 'required | integer',
            'sku_id'   => 'required | integer',
            'user_id'  => 'required | integer',
            'amount'   => 'required | integer'
        ]);
        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        //加入购物车
        if($cartModel -> api_add_cart($request->all())){
            return self::response(null,self::SUC_CODE,'加入购物车成功');
        }
        return self::response(null,self::ERROR_CODE,Cart::$error);
    }

    /**购物车列表的获取
     * @param Request $request
     * @param Cart $cartModel
     * @return mixed
     */
    public function cart_list(Request $request,Cart $cartModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'  => 'required | integer',
        ]);

        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if($list = $cartModel -> api_cart_list($request->input('user_id'))){
            return self::response($list,self::SUC_CODE,'获取数据成功');
        }
        return self::response(null,self::ERROR_CODE,Cart::$error);
    }

    /**
     * 更新购物车
     * * $post_data = [
            ['goods_id'=>1,'sku_id'=>1,'amount'=>2],
            ['goods_id'=>1,'sku_id'=>2,'amount'=>1],
         ];
     */
    public function updateCart(Request $request,Cart $cartModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'   => 'required | integer',
            'cart_list.*.goods_id' => 'required | integer',
            'cart_list.*.sku_id'   => 'required | integer',
            'cart_list.*.amount'   => 'required | integer',
        ]);

        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if($cartModel -> api_update_cart($request->input('user_id'),$request->input('cart_list'))){
            return self::response(null,self::SUC_CODE,'更新购物车成功');
        }
        return self::response(null,self::ERROR_CODE,Cart::$error);
    }

    /**
     * 立即购买和购物车结算统一调用接口
     * $post_data = [
            ['sku_id'=>1,'amount'=>2],
            ['sku_id'=>2,'amount'=>1],
       ];
     */
    public function buyNow(Request $request,Cart $cartModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'   => 'required | integer',
            'goods_list' => 'required',
            'goods_list.*.sku_id'   => 'required | integer',
            'goods_list.*.amount'   => 'required | integer',
        ]);

        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if($list = $cartModel -> api_buy_now($request->input('user_id'),$request->input('goods_list'))){
            return self::response($list,self::SUC_CODE,'获取数据成功');
        }

        return self::response($list,self::ERROR_CODE,Cart::$error);
    }

    /**
     * 购物车商品的删除
     * $request = ['user_id'=>21,'sku_id'=>[1,2,3,4]];
     */
    public function delete(Request $request,Cart $cartModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'   => 'required | integer',
            'sku_id'    => 'required | array',
        ]);

        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if($list = $cartModel -> api_delete($request->input('user_id'),$request->input('sku_id'))){
            return self::response(null,self::SUC_CODE,'删除成功');
        }
    }

    /**
     *购物车商品数量的增加
     * $request = ['user_id'=>21,'sku_id'=>[1,2,3,4]];
     */
    public function increment(Request $request,Cart $cartModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'   => 'required | integer',
            'sku_id'    => 'required',
        ]);
        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if($list = $cartModel -> api_increment($request->input('user_id'),$request->input('sku_id'))){
            return self::response(null,self::SUC_CODE,'增加购物车商品数量成功');
        }

        return self::response(null,self::ERROR_CODE,Cart::$error);
    }

    /**购物车商品数量的减少
     * @param Request $request
     * @param Cart $cartModel
     */
    public function decrement(Request $request,Cart $cartModel)
    {
        $validator = \Validator::make($request->all(),[
            'user_id'   => 'required | integer',
            'sku_id'    => 'required',
        ]);
        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if($list = $cartModel -> api_decrement($request->input('user_id'),$request->input('sku_id'))){
            return self::response(null,self::SUC_CODE,'减少购物车商品数量成功');
        }

        return self::response(null,self::ERROR_CODE,Cart::$error);
    }
}
